package com.uy.cuantoconocesdeuruguay.utiles;

import java.util.ArrayList;

import android.app.Activity;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.uy.cuantoconocesdeuruguay.view.AutoResizeTextView;

public class StringUtils {
	private static final ArrayList<AutoResizeTextView> textViewList = new ArrayList<AutoResizeTextView>();
	public static void formatStringAnswer(String answer, TextView textView){
		String formattedWord = StringPool.BLANK;
		String[] words = answer.split(StringPool.SPACE);
		String longest = getLongestString(words);
		int lines = 1;
		int currentLength = 0;
		boolean mustContinue = true;
		for (String word : words){
			mustContinue = !word.equals(words[words.length-1]);
			if (currentLength < longest.length()){
				formattedWord += word;
				currentLength += word.length(); 
				if (mustContinue){
					formattedWord += StringPool.SPACE;
					++currentLength;
				}
			}else{
				formattedWord = formattedWord.substring(0, formattedWord.length()-1);
				formattedWord += StringPool.LINE_FEED;
				++lines;
				formattedWord += word;
				currentLength = word.length(); 
				if (mustContinue){
					formattedWord += StringPool.SPACE;
					++currentLength;
				}
			}
		}
		textView.setText(formattedWord);
		textView.setMaxLines(lines);
		textViewList.add((AutoResizeTextView) textView);
	}
	
	private static String getLongestString(String[] stringArray){
		String longest = StringPool.BLANK;
		for (String string : stringArray){
			if (string.length() > longest.length()){
				longest = string;
			}
		}
		return longest;
	}

	public static void setUnifiedTextSize(Activity activity) {
		float minSize = Integer.MAX_VALUE;
		for (AutoResizeTextView textView : textViewList){
			if (textView.getTextSize() < minSize){
				minSize = textView.getTextSize();
			}
		}
		for (AutoResizeTextView textView : textViewList){
			textView.setTextSize(MetricsUtiles.convertSpToPixel(minSize, activity));
			LayoutParams params = textView.getLayoutParams();
			params.height = LayoutParams.MATCH_PARENT;
			params.width = LayoutParams.MATCH_PARENT;
			textView.setLayoutParams(params);
			//textView.invalidate();
		}
		textViewList.clear();
	}
}
